package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.query.CarPackageAuditQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Task;
import org.activiti.image.ProcessDiagramGenerator;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.zip.ZipInputStream;


@Service
@Transactional
public class CarPackageAuditServiceImpl implements ICarPackageAuditService {

    @Autowired
    private CarPackageAuditMapper carPackageAuditMapper;
    @Autowired
    private IServiceItemService serviceItemService ;
    @Autowired
    private IBpmnInfoService bpmnInfoService ;
    @Autowired
    private IUserService userService ;
    @Autowired
    private TaskService taskService ;
    @Autowired
    private RuntimeService runtimeService ;
    @Autowired
    private RepositoryService repositoryService ;




    @Override
    public TablePageInfo<CarPackageAudit> query(CarPackageAuditQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List<CarPackageAudit> carPackageAudits = carPackageAuditMapper.selectForList(qo);
        for (CarPackageAudit carPackageAudit : carPackageAudits) {
            carPackageAudit.setBpmnInfo(bpmnInfoService.get(carPackageAudit.getBpmnInfoId()));
            carPackageAudit.setServiceItem(serviceItemService.get(carPackageAudit.getServiceItemId()));
            carPackageAudit.setAuditor(userService.get(carPackageAudit.getAuditorId()));

        }
        return new TablePageInfo<CarPackageAudit>(carPackageAudits);
    }

    @Override
    public void save(CarPackageAudit carPackageAudit) {
        carPackageAuditMapper.insert(carPackageAudit);
    }

    @Override
    public CarPackageAudit get(Long id) {
        return carPackageAuditMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(CarPackageAudit carPackageAudit) {
        carPackageAuditMapper.updateByPrimaryKey(carPackageAudit);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            carPackageAuditMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<CarPackageAudit> list() {
        return carPackageAuditMapper.selectAll();
    }

    @Override
    public InputStream getProcessImageByAuditId(Long auditId) {
        CarPackageAudit carPackageAudit = this.get(auditId);
        BpmnInfo bpmnInfo = bpmnInfoService.get(carPackageAudit.getBpmnInfoId());
        BpmnModel bpmnModel = repositoryService.getBpmnModel(bpmnInfo.getActProcessId());
        DefaultProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
        InputStream inputStream=null;

        if(CarPackageAudit.STATUS_IN_ROGRESS.equals(carPackageAudit.getStatus())){
            Task task = taskService.createTaskQuery().processInstanceId(carPackageAudit.getInstanceId()).singleResult();
            List<String> activeActivityIds = runtimeService.getActiveActivityIds(task.getExecutionId());

            inputStream = generator.generateDiagram(bpmnModel, activeActivityIds, Collections.EMPTY_LIST,"宋体","宋体","宋体");
        }else {
            inputStream = generator.generateDiagram(bpmnModel, Collections.EMPTY_LIST,Collections.EMPTY_LIST,"宋体","宋体","宋体");
        }


        return inputStream;
    }

    @Override
    public void cancelApply(Long id) {
        CarPackageAudit audit = this.get(id);
        if(!CarPackageAudit.STATUS_IN_ROGRESS.equals(audit.getStatus())){
            throw new BusinessException("只有在审核中的记录才允许撤销");
        }
        serviceItemService.changeAuditStatus(audit.getServiceItemId(), ServiceItem.AUDITSTATUS_INIT);
        carPackageAuditMapper.changeStatus(id,CarPackageAudit.STATUS_CANCEL);
        runtimeService.deleteProcessInstance(audit.getInstanceId(), "撤销审核");

    }

    @Override
    public void updateInstanceId(Long id, String instanceId) {
        carPackageAuditMapper.updateInstanceId(id,instanceId);
    }


}
